package com.cat.admin.security;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cat.admin.service.SysUserService;
import com.cat.common.entity.SysUser;
import com.cat.common.exception.BaseException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author dd-cat
 * @version 1.0
 * @date 2020-10-12 16:48
 */
@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, username);
        SysUser user = sysUserService.getOne(queryWrapper);
        if (user == null) {
            throw new UsernameNotFoundException("");
        }
        if (!"0".equals(user.getEnabled())) {
            throw new BaseException("账号未激活！");
        }
        return sysUserService.getUserInfo(user);
    }

}
